package com.vcredit.creditcat.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.AuthItemEnum;
import com.vcredit.creditcat.model.AuthItem;
import com.vcredit.creditcat.model.ProductItem;
import com.vcredit.creditcat.persistence.base.AuthItemMapper;

/**
 * 
 **/
@Service
public class AuthItemService {
	
	@Autowired
	private ProductItemService productItemService;
	
	@Autowired
	private AuthItemMapper mapper;

	@CacheEvict(key = "'config:AuthItems'", value = "authitem")
	public void insert(AuthItem obj) {
		mapper.insert(obj);
	}

	@CacheEvict(key = "'config:AuthItems'", value = "authitem")
	public void update(AuthItem obj) {
		mapper.update(obj);
	}

	@Cacheable(key = "'config:AuthItems'", value = "authitem")
	public Map<Long, AuthItem> findAll() {
		Map<Long, AuthItem> AuthItems = new HashMap<Long, AuthItem>();
		for (AuthItem authItem : mapper.findAll()) {
			AuthItems.put(authItem.getItemId(), authItem);
		}
		return AuthItems;
	}

	public List<AuthItem> find(AuthItem obj) {
		return mapper.find(obj);
	}

	/**   
	 * @Title: getItemByCodes   
	 * @Description: 查找已包含的认证项   
	 * @param: @param codes
	 * @param: @return      
	 * @return: List<AuthItem>      
	 * @throws   
	 */
	public List<AuthItem> getItemByCodes(int codes) {
		Map<Long, AuthItem> authItems = this.findAll();
		Iterator<AuthItem> it = authItems.values().iterator();
		List<AuthItem> list = new ArrayList<AuthItem>();
		while (it.hasNext()) {
			AuthItem obj = it.next();
			// 排除实名认证
			if (obj.getAuthCode() != AuthItemEnum.REAL_NAME.getCode() 
					&& (codes & obj.getAuthCode()) == obj.getAuthCode()) {
				list.add(obj);
			}
		}
		Collections.sort(list, new Comparator<AuthItem>() {
			@Override
			public int compare(AuthItem o1, AuthItem o2) {
				return o1.getItemSort() - o2.getItemSort();
			}
		});
		return list;
	}

	/**   
	 * @Title: findUnAuthItems   
	 * @Description: 查找未包含的认证项 
	 * @param: @param code
	 * @param: @return      
	 * @return: List<AuthItem>      
	 * @throws   
	 */
	public List<AuthItem> findUndoAuthItems(int code) {
		Map<Long, AuthItem> authItems = this.findAll();
		Iterator<AuthItem> it = authItems.values().iterator();
		List<AuthItem> list = new ArrayList<AuthItem>();
		while (it.hasNext()) {
			AuthItem obj = it.next();
			if (0==(code & obj.getAuthCode())) {
				list.add(obj);
			}
		}
		return list;
	}
	
	
	public List<AuthItem> findAllAuthItems() {
		Map<Long, AuthItem> authItems = this.findAll();
		Iterator<AuthItem> it = authItems.values().iterator();
		List<AuthItem> list = new ArrayList<AuthItem>();
		while (it.hasNext()) {
			AuthItem obj = it.next();
			list.add(obj);
		}
		return list;
	}
	
	public AuthItem getItemByCode(int code){
		Map<Long, AuthItem> authItems = findAll();
		Iterator<AuthItem> it = authItems.values().iterator();
		while (it.hasNext()) {
			AuthItem obj = it.next();
			if (obj.getAuthCode().intValue() == code) {
				return obj;
			}
		}
		return null;
	}
	
	public AuthItem getItemById(Long id) {
		Map<Long, AuthItem> itemMap = this.findAll();
		return itemMap.get(id);
	}
	
	public List<AuthItem> getAuthItemsByProduct(long productId) {
		ProductItem productItem = productItemService.findByProductId(productId);
		return this.getItemByCodes(productItem.getMinCode());
	}
}